package org.example.algorithm;

/**
 * 循环数组实现队列
 * todo:不用size变量实现
 */

public class MyCircularQueueSolution {
    private int capacity;
    private int size;
    private int[] array;
    private int enQueuePointer;
    private int deQueuePointer;

    public static void main(String[] args) {
        MyCircularQueueSolution myCircularQueueSolution = new MyCircularQueueSolution(3);
        myCircularQueueSolution.enQueue(1);
        myCircularQueueSolution.enQueue(2);
        myCircularQueueSolution.enQueue(3);
        myCircularQueueSolution.enQueue(4);
        int last = myCircularQueueSolution.Rear();
        System.out.println(last);
        myCircularQueueSolution.isFull();
        myCircularQueueSolution.deQueue();
        myCircularQueueSolution.enQueue(4);
        last = myCircularQueueSolution.Rear();
        System.out.println(last);
    }

    public MyCircularQueueSolution(int k) {
        this.capacity = k;
        this.size = 0;
        this.array = new int[capacity];
    }
    public boolean enQueue(int value) {
        if (this.size < this.capacity) {
            array[this.enQueuePointer] = value;
            this.size++;
            this.enQueuePointer++;
            if (this.enQueuePointer > (this.capacity-1)) {
                this.enQueuePointer = 0;
            }
            return true;
        }
        return false;
    }

    public boolean deQueue() {
        if (this.size > 0) {
            this.size--;
            this.deQueuePointer++;
            if (this.deQueuePointer > (this.capacity-1)) {
                this.deQueuePointer = 0;
            }
            return true;
        }
        return false;
    }

    public int Front() {
        if (this.size == 0) {
            return -1;
        }
        return array[this.deQueuePointer];
    }

    public int Rear() {
        if (this.size == 0) {
            return -1;
        }
        if (this.enQueuePointer == 0) {
            return array[this.capacity - 1];
        }
        return array[this.enQueuePointer - 1];
    }

    public boolean isEmpty() {
        return this.size == 0;
    }

    public boolean isFull() {
        return this.size == this.capacity;
    }
}
